package com.tchintech.pay.view.activity;


import android.app.Activity;
import android.os.Handler;

import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.tchintech.pay.R;
import com.tchintech.pay.delegate.QuickPayThreeDeleagte;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.view.fragment.MainFragment;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/12 0012.
 * 银联快捷支付界面
 */

public class QuickPayThreeActivity extends ActivityPresenter<QuickPayThreeDeleagte> implements View.OnClickListener {
    private WebView webView;
    private TextView ddje, shmc, jysj, ddbh;
    private JSONObject jsonObject;
    private Handler handler;


    @Override
    protected void initView() {
        super.initView();
        viewDelegate.setTitle("快捷支付");
        viewDelegate.setVisible();
        webView = viewDelegate.getWebview();
        viewDelegate.setOnClickListener(this, R.id.iv_back);
        inintTextView();


    }


    @Override
    protected void initData() {
        super.initData();

        String data = getIntent().getStringExtra("data");
        try {
            jsonObject = new JSONObject(data);
            jsonObject = jsonObject.getJSONObject("msg");
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private void inintTextView() {
        ddje = viewDelegate.getTv(R.id.tv_ddje);
        shmc = viewDelegate.getTv(R.id.tv_shmc);
        jysj = viewDelegate.getTv(R.id.tv_jysj);
        ddbh = viewDelegate.getTv(R.id.tv_ddbh);
        //设置值
        try {
            ddje.setText(jsonObject.getString("amount") + "元");
            shmc.setText(jsonObject.getString("name"));
            jysj.setText(jsonObject.getString("time"));
            ddbh.setText(jsonObject.getString("ordernumber"));
            setWebView(jsonObject.getString("url"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    //加载url
    private void setWebView(String url) {
        WebSettings webSettings = webView.getSettings();
        //使用js展示js网页特效
        webSettings.setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }


        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    hideProgressDialog();
                } else {
                    if (pd == null) {
                        showProgressDialog("", "加载中，请稍后...");
                    }
                }
            }
        });
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        //最后加载url
        webView.loadUrl(url);
        webView.addJavascriptInterface(this, "quickpay");

    }

    /**
     * 映射到js的方法，供js界面调用
     */
    @JavascriptInterface
    public void finishAll() {
        MainFragment.listA.get(0).finish();
        MainFragment.listA.clear();
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            // 返回键退回
            webView.goBack();
            if (pd.isShowing()) {
                hideProgressDialog();
                return true;
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @Override
    protected Class<QuickPayThreeDeleagte> getDelegateClass() {
        return QuickPayThreeDeleagte.class;
    }


}

